package cn.engage.pet.controller;

import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.LoginContext;
import cn.engage.basic.util.PageList;
import cn.engage.org.domain.Employee;
import cn.engage.org.service.EmployeeService;
import cn.engage.pet.domain.SearchMasterMsg;
import cn.engage.pet.dto.AcceptDto;
import cn.engage.pet.query.SearchMasterMsgQuery;
import cn.engage.pet.service.SearchMasterMsgService;
import cn.engage.user.domain.Logininfo;
import cn.engage.user.domain.User;
import cn.engage.user.service.UserService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public SearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserService userService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id) {
        return searchMasterMsgService.findByIdOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){
        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryByPage(query);
    }

    /* 宠物寻主,用户发布宠物信息，后端保存到数据库 */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.findBylogininfoId(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());
        return searchMasterMsgService.publish(searchMasterMsg);
    }


    /* 待发布，没有审核通过的数据 state = 0 */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
                query.setState(0);
        return searchMasterMsgService.findSearchMsgQuery(query);
    }

    /* 审核通过-未分配-寻主池,查询寻主池数据 state=1 & Shop=null */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query) {
        query.setState(1);
        return searchMasterMsgService.findSearchMsgQuery(query);
    }

    /* 审核通过-已分配 state=1 & shopId=当前登陆人店铺Id */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        query.setState(1);
        return searchMasterMsgService.findSearchMsgQuery(query);
    }

    /* 订单已完成 state=3 1.平台管理员：shopId=null  2.店铺管理员：shopId=当前登陆人shopId*/
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee emp = employeeService.findByLogininfoId(logininfo.getId());
        if (emp.getShopId() != null){
            query.setShopId(emp.getShopId());
        }
        query.setState(3);
        return searchMasterMsgService.findSearchMsgQuery(query);
    }

    /* 接收订单 */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto) {
        searchMasterMsgService.accept(acceptDto);
        return AjaxResult.me().success();
    }

    /* 拒单 */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        searchMasterMsgService.reject(msgId, employee);
        return AjaxResult.me().success();
    }
}
